package com.maijinjie.admin.content.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.maijinjie.common.bean.Pagination;
import com.maijinjie.common.bean.StatusBean;
import com.maijinjie.admin.content.bean.ArticalNewsBean;
import com.maijinjie.admin.content.dao.ArticalNewsMapper;
import com.maijinjie.admin.content.field.ArticalNewsFields;
import com.maijinjie.admin.content.service.ArticalNewsService;
import com.maijinjie.common.service.impl.BaseServiceImpl;

/**
 * ArticalNewsServiceImpl
 * @author 罗正杰
 **/
@Service("articalNewsService")
public class ArticalNewsServiceImpl extends BaseServiceImpl implements ArticalNewsService{
	@Resource
	ArticalNewsMapper articalNewsMapper;
	
	@Override
	public ArticalNewsBean getBeanById(Integer id) throws Exception{
		if(null==id || 0>=id){
			return null;
		} else {
			return (ArticalNewsBean) getBeanById(articalNewsMapper, id);
		}
	}
	@Override
	public int count(Map<String, Object> params) throws Exception {
		return articalNewsMapper.count(params);
	}
	@Override
	@Transactional
	public List<ArticalNewsBean> list(Map<String, Object> params) throws Exception {
		return (List<ArticalNewsBean>) articalNewsMapper.list(params);
	}
	@Override
	public StatusBean save(ArticalNewsBean bean, String createBy, Integer createUserId) throws Exception {
		Date currDate = new Date();
		//TODO 处理业务
		
		if(0<articalNewsMapper.save(bean)){
			return success("操作成功！");
		}
		return error("操作失败！");
	}
	@Override
	public StatusBean update(ArticalNewsBean bean, String createBy, Integer createUserId) throws Exception {
		if(null!=bean){
			Date currDate = new Date();
			HashMap<String, Object> params = new HashMap<String, Object>();
			//TODO 处理业务
			params.put(ArticalNewsFields.id.toString(), bean.getId());
			params.put(ArticalNewsFields.title.toString(), bean.getTitle());
			params.put(ArticalNewsFields.content.toString(), bean.getContent());
			params.put(ArticalNewsFields.description.toString(), bean.getDescription());
			params.put(ArticalNewsFields.specifyDate.toString(), bean.getSpecifyDate());
			params.put(ArticalNewsFields.descriptionCn.toString(), bean.getDescriptionCn());
			params.put(ArticalNewsFields.descriptionEn.toString(), bean.getDescriptionEn());
			params.put("sent1", bean.getSent1());
			params.put("sent2", bean.getSent2());
			params.put("sent3", bean.getSent3());
			params.put("picPath", bean.getPicPath());
			params.put(ArticalNewsFields.lastUpdateDate.toString(), currDate);
			params.put(ArticalNewsFields.lastUpdateUserId.toString(), createUserId);
			if(articalNewsMapper.update(params)){
				return success("操作成功！");
			}
		}
		return error("操作失败！");
	}
	@Override
	public StatusBean deleteById(Integer id) throws Exception {
		if(null!=id && 0<id){
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put(ArticalNewsFields.id.toString(), id);
			if(articalNewsMapper.delete(params)){
				return success("操作成功！");
			}
		}
		return error("操作失败！");
	}
	@Override
	public Pagination<ArticalNewsBean> list(Pagination<ArticalNewsBean> pagination) throws Exception {
		return (Pagination<ArticalNewsBean>) getPagination(articalNewsMapper, pagination);
	}
	@Override
	public StatusBean batchDelete(String ids) throws Exception {
		if(null!=ids && !"".equals(ids.trim())) {
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put(ArticalNewsFields.id.toString(), ids);
			if(articalNewsMapper.batchDelete(params)){
				return success("操作成功！");
			}
		}
		return error("操作失败！");
	}
	@Override
	public List<ArticalNewsBean> listAll() throws Exception {
		return (List<ArticalNewsBean>) articalNewsMapper.list(null);
	}
	@Override
	public Map<String, List> listWithDate(Map<String, Object> params) throws Exception {
		params.put("orderby", "specify_date");
		List<ArticalNewsBean> list = (List<ArticalNewsBean>) this.articalNewsMapper.list(params);
		List<String> yearList = new ArrayList<String>();
		List<ArticalNewsBean> temp = new ArrayList<ArticalNewsBean>();
		List<List<ArticalNewsBean>> newsList = new ArrayList<List<ArticalNewsBean>>();
		Map<String,List> map = new HashMap<String,List>();
		map.put("year",yearList);
		map.put("result", newsList);
		for(ArticalNewsBean bean : list){
			String specifyDate = bean.getSpecifyDate();
			if(!StringUtils.isEmpty(specifyDate)){
				String year = specifyDate.substring(0,4);
				boolean exist = false;
				for(String y : yearList){
					if(y.equals(year)){
						exist = true;
					}
				}
				if(!exist){
					yearList.add(year);
					if(temp.size() > 0){
						newsList.add(temp);
						temp = new ArrayList<ArticalNewsBean>();
					}
					temp.add(bean);
				}else{
					temp.add(bean);
				}
			}
		}
		newsList.add(temp);
		return map;
	}
}
